package com.sanjishu.enterlab.electricity.sale.controller.manage;

import com.sanjishu.enterlab.electricity.sale.controller.Response;
import com.sanjishu.enterlab.electricity.sale.domain.CompanyInfo;
import com.sanjishu.enterlab.electricity.sale.domain.MonthElecData;
import com.sanjishu.enterlab.electricity.sale.domain.User;
import com.sanjishu.enterlab.electricity.sale.service.CompanyInfoService;
import com.sanjishu.enterlab.electricity.sale.service.MonthELECDataService;
import com.sanjishu.enterlab.electricity.sale.service.MonthUserDataService;
import com.sanjishu.enterlab.electricity.sale.service.UserService;
import com.sanjishu.enterlab.electricity.sale.utils.ImportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manage/monthData")
public class MonthELECDataController {

    @Autowired
    private MonthELECDataService monthELECDataService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private MonthUserDataService monthUserDataService;

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(Model model) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        List<MonthElecData> monthData = monthELECDataService.listByMonth(year, month);
        model.addAttribute("year", year);
        model.addAttribute("month", month);
        model.addAttribute("monthData", monthData);
        return "manage/monthData/index";
    }

    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String edit(Integer id, Model model) {
        MonthElecData monthElecData = monthELECDataService.show(id);
        model.addAttribute("monthElecData", monthElecData);
        return "manage/monthData/editTab";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Response update(Integer id, String companyName, String userName, double saveElecNum,
                           double elecConsumption, double elecActual, double priceDifference,
                           double agencyFee, int year, int month, Model model) {
        CompanyInfo companyInfo = companyInfoService.getByName(companyName);
        if (companyInfo == null) {
            return Response.getError("当前公司不在系统内。");
        }
        User user = userService.findByPhone(userName);
        if (user == null) {
            return Response.getError("当前销售不在系统内。");
        }
        monthELECDataService.update(id, companyInfo.getId(), user.getId(), saveElecNum, elecConsumption,
                elecActual, priceDifference, agencyFee, year, month);

        return Response.buildSuccess("修改成功。");
    }

    @RequestMapping(value = "search/{year}/{month}", method = RequestMethod.GET)
    public String searchData(@PathVariable Integer year, @PathVariable Integer month, Model model) {
        List<MonthElecData> monthData = monthELECDataService.listByMonth(year, month);
        model.addAttribute("year", year);
        model.addAttribute("month", month);
        model.addAttribute("monthData", monthData);
        return "manage/monthData/index";
    }

    @PostMapping("generateUserData")
    @ResponseBody
    public Response deviationStatistics(Integer year, Integer month) {
        monthUserDataService.updateUserDatas(year, month);
        return Response.buildSuccess("");
    }

    @PostMapping("import")
    @ResponseBody
    public Response importAccident(@RequestParam("file") MultipartFile file,
                                   HttpServletRequest request) throws Exception {
        List<MonthElecData> monthElecDataList = null;
        //try {
            monthElecDataList = monthELECDataService.checkImport(file);
        //} catch (ImportException e) {
        //    return Response.getError(e.getMessage());
        //}
        monthELECDataService.import2DB(monthElecDataList);
        return Response.buildSuccess("导入成功");
    }

}
